import 'package:flutter/material.dart';
import 'package:read_book/data/book_mall/book_mall.dart';
import 'package:read_book/ui/widget/book_des.dart';

class ClassifyInfo extends StatefulWidget {
  String gender;
  Map data;

  ClassifyInfo({Key key, this.gender, this.data}) : super(key: key);

  @override
  ClassifyInfoState createState() => ClassifyInfoState();
}

class ClassifyInfoState extends State<ClassifyInfo> {
  Map<dynamic, dynamic> data = {};
  String type = 'hot';
  var hotStyle = Colors.green[500];
  var nStyle = Colors.black87;
  var overStyle = Colors.black87;
  List books = [];

  //获取分类小说列表
  void getTypeData() {
    setState(() {
      books = [];
    });
    Map<String, dynamic> params = {
      'gender': widget.gender,
      'major': widget.data['name'],
      'type': type,
      'minor': '',
      'start': '0',
      'limit': '20'
    };
    BookMall().getTypeData(params).then((value) {
      setState(() {
        books = value['books'];
      });
    });
  }

  @override
  void initState() {
    getTypeData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 4,
      child: Scaffold(
        appBar: AppBar(
          leading: IconButton(
            icon: const Icon(Icons.keyboard_backspace),
            onPressed: () {
              Navigator.pop(context);
            },
            color: Colors.black54,
          ),
          actions: <Widget>[
            IconButton(
              icon: const Icon(Icons.search),
              onPressed: () {
                Navigator.pushNamed(context, '/search');
              },
              color: Colors.black54,
            )
          ],
          backgroundColor: Colors.grey[50],
          title: Center(
            child: Text(
              widget.data['name'],
              style: const TextStyle(color: Colors.black54, fontSize: 16.0),
            ),
          ),
        ),
        body: Container(
          alignment: Alignment.center,
          child: Column(
            children: <Widget>[
              SizedBox(
                height: 40.0,
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            type = 'hot';
                            hotStyle = Colors.green[500];
                            nStyle = Colors.black87;
                            overStyle = Colors.black87;
                          });
                          getTypeData();
                        },
                        child: Text('热门',
                            textAlign: TextAlign.center,
                            style: TextStyle(color: hotStyle)),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            type = 'new';
                            hotStyle = Colors.black87;
                            nStyle = Colors.green[500];
                            overStyle = Colors.black87;
                          });
                          getTypeData();
                        },
                        child: Text(
                          '新书',
                          textAlign: TextAlign.center,
                          style: TextStyle(color: nStyle),
                        ),
                      ),
                    ),
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            type = 'over';
                            hotStyle = Colors.black87;
                            nStyle = Colors.black87;
                            overStyle = Colors.green[500];
                          });
                        },
                        child: Text(
                          '完结',
                          textAlign: TextAlign.center,
                          style: TextStyle(color: overStyle),
                        ),
                      ),
                    )
                  ],
                ),
              ),
              Flexible(
                child: ListView(
                  children: <Widget>[buildList(books)],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget buildList(List books) {
    return ListView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemCount: books.length,
      itemBuilder: (BuildContext context, int index) {
        return Container(
          padding: const EdgeInsets.only(left: 10.0),
          child: BookDes(book: books[index]),
          decoration: const BoxDecoration(
              border: Border(
                  bottom: BorderSide(width: 1, color: Color(0xffe5e5e5)))),
        );
      },
    );
  }
}
